package com.feng.gaoerfu.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

/**
 * SharePreferences操作助手类
 * @author zhanlg
 *
 */
public class SharedPreferencesHelper {

	private SharedPreferences sp;
	private SharedPreferences.Editor editor;
	private static final String PREF_NAME = "CigaretteNet";
	private static SharedPreferencesHelper spHelper;
	
	@SuppressLint("CommitPrefEdits")
	private SharedPreferencesHelper(Context context){
		sp = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
		editor = sp.edit();
	}
	
	/**
	 * 数据存储助手单例模式
	 * @param context
	 * @return spHelper
	 */
	public static SharedPreferencesHelper getInstance(Context context){
		if(null == spHelper){
			spHelper = new SharedPreferencesHelper(context);
		}
		return spHelper;
	}
	
	/**
	 * 清理（删除）SP缓存文件
	 */
	public void ClearSPData(){
		editor.clear();
		editor.commit();
	}
	
	/**
	 * putStringValue
	 * @param key
	 * @param value
	 */
	public void putStringValue(String key, String value){
		editor = sp.edit();
		editor.putString(key, value);
		editor.commit();
	}
	
	/**
	 * putBooleanValue
	 * @param key
	 * @param value
	 */
	public void putBooleanValue(String key, Boolean value){
		editor = sp.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}
	
	/**
	 * putIntegerValue
	 * @param key
	 * @param value
	 */
	public void putIntegerValue(String key, Integer value){
		editor = sp.edit();
		if(null == value){
			value = 0;
		}
		editor.putInt(key, value);
		editor.commit();
	}
	
	/**
	 * putLongValue
	 * @param key
	 * @param value
	 */
	public void putLongValue(String key, Long value){
        editor = sp.edit();
        if (value == null)
        {
            value = 0l;
        }
        editor.putLong(key, value);
        editor.commit();
    }
	
	/**
	 * getStringValue
	 * @param key
	 * @return sp.getString(key, null)
	 */
	 public String getStringValue(String key){
	     return sp.getString(key, null);
	 }
	
	 /**
	  * getBooleanValue
	  * @param key
	  * @return sp.getBoolean(key, false);
	  */
	 public Boolean getBooleanValue(String key){
	     return sp.getBoolean(key, false);
	 }
	 /**
	  * getBooleanValue
	  * @param key
	  * @return sp.getBoolean(key, false);
	  */
	 public Boolean getBooleanValue(String key, boolean bl){
	     return sp.getBoolean(key, bl);
	 }

	 /**
	  * getIntegerValue
	  * @param key
	  * @return return sp.getInt(key, 0);
	  */
	 public int getIntegerValue(String key){
	     return sp.getInt(key, 0);
	 }
	 
	 /**
	  * getLongValue
	  * @param key
	  * @return sp.getLong(key, 0l);
	  */
	 public long getLongValue(String key){
	     return sp.getLong(key, 0l);
	 }
}
